package com.file.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Collector;
import com.entity.Measuringpoint;
import com.entity.Org;
import com.entity.Terminal;
import com.file.entity.FileTerminal;
import com.interfaces.CollectorDao;
import com.interfaces.MeasuringpointDao;
import com.interfaces.MeterDao;
import com.interfaces.OrgDao;
import com.interfaces.SysdicDao;
import com.interfaces.TerminalDao;
import com.setting.service.SysdicService;
import com.utils.JsonResult;
import com.utils.Utils;

/**
 * @Title: TerminalService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月24日 上午11:36:45
 * @version V1.0.0
 */

@Service
public class TerminalService {

	private static final Logger log = LoggerFactory.getLogger(TerminalService.class);

	@Autowired
	private SysdicDao sysdicdao; // 系统字典

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private MeasuringpointDao measuringpointdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private CollectorDao collectordao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private OrgDao orgdao;

	// get列表
	@Transactional
	public JsonResult<List<FileTerminal>> listTerminal(String opeorgcookie, String opetypecookie,
			String languagecookie) {

		List<Terminal> terminals = terminaldao
				.findByTerminalorgAndTypeAndTerminalstatusNot(Integer.valueOf(opeorgcookie), opetypecookie, "4");
		List<FileTerminal> fileterminals = new ArrayList<FileTerminal>();
		if (terminals.size() != 0) {
			for (int i = 0; i < terminals.size(); i++) {

				Terminal terminal = terminals.get(i);
				fileterminals.add(dofileterminal(terminal, languagecookie));
			}
		}
		log.info("listTerminalSuccess");
		return new JsonResult<List<FileTerminal>>(123, "展示集中器所有列表", fileterminals);

	}

	// 条件列表
	@Transactional
	public JsonResult<List<FileTerminal>> listTerminalByConditions(String terminalcode, String terminalassetcode,
			String terminaltype, String terminaladdress, String terminalcommuncate, String terminalmade,
			String opeorgcookie, String terminalstatus, String opetypecookie, HttpServletRequest request,
			String languagecookie) {

		if (Utils.isNull(terminalcode) && Utils.isNull(terminalassetcode) && Utils.isNull(terminaltype)
				&& Utils.isNull(terminaladdress) && Utils.isNull(terminalcommuncate) && Utils.isNull(terminalmade)
				&& Utils.isNull(terminalstatus)) {
			List<FileTerminal> fileterminals = new ArrayList<FileTerminal>();
			if (opeorgcookie.equals("1")) {
				List<Terminal> terminals = terminaldao.findByTypeAndTerminalstatusNot(opetypecookie, "4");
				for (Terminal terminal : terminals) {
					fileterminals.add(dofileterminal(terminal, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Terminal> terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
							Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");

					for (Terminal terminal : terminals) {
						fileterminals.add(dofileterminal(terminal, languagecookie));
					}
				}
			} else {
				List<Terminal> terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
						Integer.valueOf(opeorgcookie), opetypecookie, "4");
				if (terminals.size() != 0) {
					for (int i = 0; i < terminals.size(); i++) {

						Terminal terminal = terminals.get(i);
						fileterminals.add(dofileterminal(terminal, languagecookie));
					}
				}
			}
			log.info("listTerminalSuccess");
			return new JsonResult<List<FileTerminal>>(123, "展示集中器所有列表", fileterminals);

		} else {
			List<FileTerminal> fileterminals = new ArrayList<FileTerminal>();
			if (opeorgcookie.equals("1")) {
				List<Terminal> terminals = terminaldao.findByConditions(terminalcode, terminalassetcode, terminaltype,
						terminaladdress, terminalcommuncate, terminalmade, null, terminalstatus, opetypecookie);
				for (Terminal terminal : terminals) {
					fileterminals.add(dofileterminal(terminal, languagecookie));
				}
			} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
				List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
				for (Org org : orgs) {
					List<Terminal> terminals = terminaldao.findByConditions(terminalcode, terminalassetcode,
							terminaltype, terminaladdress, terminalcommuncate, terminalmade,
							Integer.parseInt(org.getOrgvalue()), terminalstatus, opetypecookie);

					for (Terminal terminal : terminals) {
						fileterminals.add(dofileterminal(terminal, languagecookie));
					}
				}
			} else {
				List<Terminal> terminals = terminaldao.findByConditions(terminalcode, terminalassetcode, terminaltype,
						terminaladdress, terminalcommuncate, terminalmade, Integer.valueOf(opeorgcookie),
						terminalstatus, opetypecookie);

				if (terminals.size() != 0) {
					for (int i = 0; i < terminals.size(); i++) {

						Terminal terminal = terminals.get(i);
						fileterminals.add(dofileterminal(terminal, languagecookie));
					}
				}
			}
			log.info("listTerminalByConditionsSuccess");
			return new JsonResult<List<FileTerminal>>(130, "展示集中器条件查询列表", fileterminals);
		}

	}

	// 增加
	@Transactional
	public JsonResult<Terminal> addTerminal(String terminalcode, String terminalassetcode, String terminaltype,
			String terminaladdress, String terminalcommuncate, String terminalmade, String opeorgcookie,
			String opetypecookie, String terminalphysicaladdress) {

		if (Utils.isNullOrEmpty(terminalcode) || Utils.isNullOrEmpty(terminalassetcode)
				|| Utils.isNullOrEmpty(terminaltype) || Utils.isNullOrEmpty(terminaladdress)
				|| Utils.isNullOrEmpty(terminalcommuncate) || Utils.isNullOrEmpty(terminalmade)
				|| Utils.isNullOrEmpty(opeorgcookie) || Utils.isNullOrEmpty(opetypecookie)) {

			return new JsonResult<Terminal>(122, "增加集中器失败..数值不能为空", null);

		} else if (terminaldao.findByTerminalcodeAndTerminalstatusNot(terminalcode, "4") != null) {

			return new JsonResult<Terminal>(131, "增加集中器失败..出厂编码不能重复", null);

		} else if (terminaldao.findByTerminalassetcodeAndTerminalstatusNot(terminalassetcode, "4") != null) {

			return new JsonResult<Terminal>(128, "增加集中器失败..资产编码不能重复", null);

		} else if (terminaldao.findByTerminaladdressAndTerminalstatusNot(terminaladdress, "4") != null) {

			return new JsonResult<Terminal>(135, "增加集中器失败..终端地址不能重复", null);
		} else {

			Terminal terminal = new Terminal();
			terminal.setTerminalcode(terminalcode);
			terminal.setTerminalassetcode(terminalassetcode);
			terminal.setTerminaltype(terminaltype);
			terminal.setTerminaladdress(terminaladdress);
			terminal.setTerminalcommuncate(terminalcommuncate);
			terminal.setTerminalmade(terminalmade);
			terminal.setTerminalorg(Integer.valueOf(opeorgcookie));
			// 增加只能增加待用的表
			terminal.setTerminalstatus("1");
			terminal.setType(opetypecookie);
			terminal.setTerminalphysicaladdress(terminalphysicaladdress);
			terminaldao.save(terminal);
			return new JsonResult<Terminal>(121, "增加集中器成功", terminal);
		}

	}

	// 删除
	@Transactional
	public JsonResult<Terminal> deleteterminal(Integer terminalid) {

		if (terminalid != null) {
			if (terminaldao.findOne(terminalid).getTerminalstatus().equals("1")) {
				// 改变采集终端状态为已拆除
				terminaldao.updateTerminal("4", terminalid);

				return new JsonResult<Terminal>(124, "删除集中器成功", null);
			}
		}
		return new JsonResult<Terminal>(134, "删除集中器失败..ID为空", null);
	}

	// 强制删除
	@Transactional
	public JsonResult<Terminal> forcedeleteterminal(Integer terminalid) {

		if (terminalid != null) {
			// 改变采集终端状态为已拆除
			terminaldao.updateTerminal("4", terminalid);

			// 删除有关采集点信息
			if (!measuringpointdao.findByTerminalid(terminalid).isEmpty()) {

				for (int i = 0; i < measuringpointdao.findByTerminalid(terminalid).size(); i++) {
					// 改变有关采集点的表状态为未运行
					Integer meterid = measuringpointdao.findByTerminalid(terminalid).get(i).getMeterid();
					meterdao.updateMeter("2", meterid);
					// 改变和该采集终端有关的采集器状态为待用
					Integer collectorid = measuringpointdao.findByTerminalid(terminalid).get(i).getCollectorid();
					collectordao.updateCollector("1", collectorid);

				}

				measuringpointdao.deleteByTerminalid(terminalid);
			}
			return new JsonResult<Terminal>(124, "删除集中器成功", null);

		}

		return new JsonResult<Terminal>(134, "删除集中器失败..ID为空", null);
	}

	// 修改页面
	@Transactional
	public JsonResult<FileTerminal> updateviewterminal(Integer terminalid, String languagecookie) {

		if (terminalid != null) {
			Terminal terminal = terminaldao.findOne(terminalid);

			return new JsonResult<FileTerminal>(125, "查单个集中器", dofileterminal(terminal, languagecookie));
		}
		return new JsonResult<FileTerminal>(125, "查单个集中器", null);
	}

	// 修改
	@Transactional
	public JsonResult<Terminal> updateTerminal(Integer terminalid, String terminalcode, String terminalassetcode,
			String terminaltype, String terminaladdress, String terminalcommuncate, String terminalmade,
			String opeorgcookie, String terminalstatus, String opetypecookie, String terminalphysicaladdress) {

		Terminal terminal = new Terminal();

		if (terminalid == null || Utils.isEmpty(terminalcode) || Utils.isEmpty(terminalassetcode)
				|| Utils.isEmpty(terminaltype) || Utils.isEmpty(terminaladdress) || Utils.isEmpty(terminalcommuncate)
				|| Utils.isEmpty(terminalmade) || Utils.isNull(opeorgcookie) || Utils.isEmpty(terminalstatus)
				|| Utils.isEmpty(opetypecookie) || Utils.isEmpty(terminalphysicaladdress)) {

			return new JsonResult<Terminal>(127, "修改集中器失败..数值不能为空", null);

		} else {

			terminal.setTerminalid(terminalid);
			terminal.setTerminalorg(Integer.valueOf(opeorgcookie));
			terminal.setType(opetypecookie);

			// 判断terminalcode
			if (Utils.isNull(terminalcode)) {
				terminal.setTerminalcode(terminaldao.findOne(terminalid).getTerminalcode());
			} else if (terminaldao.findByTerminalcodeAndTerminalstatusNot(terminalcode, "4") != null
					&& !(terminaldao.findOne(terminalid).getTerminalcode().equals(terminalcode))) {
				log.info("UpdateFail");
				return new JsonResult<Terminal>(132, "修改集中器失败..出厂编码不能重复", null);
			} else {
				terminal.setTerminalcode(terminalcode);
			}
			// 判断terminalassetcode
			if (Utils.isNull(terminalassetcode)) {
				terminal.setTerminalassetcode(terminaldao.findOne(terminalid).getTerminalassetcode());

			} else if (terminaldao.findByTerminalassetcodeAndTerminalstatusNot(terminalassetcode, "4") != null
					&& !(terminaldao.findOne(terminalid).getTerminalassetcode().equals(terminalassetcode))) {
				log.info("UpdateFail");
				return new JsonResult<Terminal>(129, "修改集中器失败..资产编码不能重复", null);
			} else {
				terminal.setTerminalassetcode(terminalassetcode);
			}
			// 判断terminaladdress
			if (Utils.isNull(terminaladdress)) {
				terminal.setTerminaladdress(terminaldao.findOne(terminalid).getTerminaladdress());

			} else if (terminaldao.findByTerminaladdressAndTerminalstatusNot(terminaladdress, "4") != null
					&& !(terminaldao.findOne(terminalid).getTerminaladdress().equals(terminaladdress))) {
				log.info("UpdateFail");
				return new JsonResult<Terminal>(136, "修改集中器失败..终端地址不能重复", null);
			} else {
				terminal.setTerminaladdress(terminaladdress);
			}

			// 判断terminaltype
			if (Utils.isNull(terminalphysicaladdress)) {

				terminal.setTerminalphysicaladdress(terminaldao.findOne(terminalid).getTerminalphysicaladdress());
			} else {
				terminal.setTerminalphysicaladdress(terminalphysicaladdress);
			}

			// 判断terminaltype
			if (Utils.isNull(terminaltype)) {

				terminal.setTerminaltype(terminaldao.findOne(terminalid).getTerminaltype());
			} else {
				terminal.setTerminaltype(terminaltype);
			}

			// 判断terminalcommuncate
			if (Utils.isNull(terminalcommuncate)) {
				terminal.setTerminalcommuncate(terminaldao.findOne(terminalid).getTerminalcommuncate());

			} else {
				terminal.setTerminalcommuncate(terminalcommuncate);
			}
			// 判断terminalmade
			if (Utils.isNull(terminalmade)) {
				terminal.setTerminalmade(terminaldao.findOne(terminalid).getTerminalmade());

			} else {
				terminal.setTerminalmade(terminalmade);
			}
			// 判断terminalstatus
			if (Utils.isNull(terminalstatus)) {
				terminal.setTerminalstatus(terminaldao.findOne(terminalid).getTerminalstatus());

			} else {
				terminal.setTerminalstatus(terminalstatus);
			}

			terminaldao.save(terminal);
			log.info("UpdateSuccess");
			return new JsonResult<Terminal>(126, "修改集中器成功", terminal);
		}

	}

	// 组装前端类
	public FileTerminal dofileterminal(Terminal terminal, String languagecookie) {

		FileTerminal fileterminal = new FileTerminal();

		fileterminal.setTerminalid(terminal.getTerminalid());
		fileterminal.setTerminalcode(terminal.getTerminalcode());
		fileterminal.setTerminalassetcode(terminal.getTerminalassetcode());
		fileterminal.setTerminaltype(terminal.getTerminaltype());
		fileterminal.setTerminaltypename(sysdicservice.finddisplay(sysdicservice.gettype(terminal.getType(), "1"),
				terminal.getTerminaltype(), languagecookie));
		fileterminal.setTerminaladdress(terminal.getTerminaladdress());
		fileterminal.setTerminalcommuncate(terminal.getTerminalcommuncate());
		fileterminal.setTerminalphysicaladdress(terminal.getTerminalphysicaladdress());
		fileterminal.setTerminalcommuncatename(
				sysdicservice.finddisplay("0007", terminal.getTerminalcommuncate(), languagecookie));
		fileterminal.setTerminalmade(terminal.getTerminalmade());
		fileterminal.setTerminalmadename(sysdicservice.finddisplay("0008", terminal.getTerminalmade(), languagecookie));
		fileterminal.setTerminalorg(terminal.getTerminalorg());
		fileterminal.setTerminalorgname(
				sysdicservice.finddisplay("0001", terminal.getTerminalorg().toString(), languagecookie));
		fileterminal.setTerminalstatus(terminal.getTerminalstatus());
		fileterminal
				.setTerminalstatusname(sysdicservice.finddisplay("0009", terminal.getTerminalstatus(), languagecookie));

		return fileterminal;
	}

	// 传入采集终端ID查询采集终端地址
	public String findTerminaladdressByTerminalid(Integer Terminalid) {

		String Terminaladdress = terminaldao.findOne(Terminalid).getTerminaladdress();
		return Terminaladdress;
	}

	public String findTerminaltypeByTerminalid(Integer Terminalid) {

		String Terminaltype = terminaldao.findOne(Terminalid).getTerminaltype();
		return Terminaltype;
	}
	
	public String findTerminalprotocolByTerminalid(Integer Terminalid) {
		
		String type =terminaldao.findOne(Terminalid).getType();//水电气热大类
		String sysdictype =sysdicservice.gettype(type, "1"); //0003
		String Terminaltype = findTerminaltypeByTerminalid(Terminalid);
		String Terminalprotocol= sysdicdao.findBySysdictypeAndSysdicvalue(sysdictype, Terminaltype).getSysdicremake();
		return Terminalprotocol;
	}

}
